package com.ys.fmms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ys.fmms.exception.UserPasswordException;
import com.ys.fmms.pojo.User;
import com.ys.fmms.service.UserService;
import com.ys.fmms.util.*;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/fmms/userController")
@Api(tags = "用户表处理")
public class UserController {

    @Resource
    private UserService userService;


    @Operation(summary = "获取用户列表")
    @GetMapping("/getUserListByPageInfo/{pageNo}/{pageSize}")
    public Result getUserListByPage(
            @PathVariable(value = "pageNo") Integer pageNo,
            @PathVariable(value = "pageSize") Integer pageSize,
            @RequestParam(value = "userName", required = false) String userName
    ) {
        Page<User> userPage = new Page<>(pageNo, pageSize);
        // records:[{},{}] List<User>
        IPage<User> userIPage = userService.getUserListByPage(userPage, userName);
        return Result.ok(userIPage);
    }

    @Operation(summary = "添加/修改用户信息")
    @PutMapping("/addOrModifyUser")
    public Result addOrModifyUser(
            @RequestBody User user,
            @RequestParam(required = false, value = "id") String userId) {
        try {
            userService.addOrModifyUser(user, userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.myFail(null).message("添加/修改用户失败");
        }
        return Result.ok();
    }

    @Operation(summary = "批量删除用户信息")
    @DeleteMapping("/deleteUserByIds")
    public Result deleteUserByIds(@RequestBody List<String> ids) {
        try {
            userService.removeByIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
        return Result.ok();
    }

    @Operation(summary = "根据token获取当前用户信息")
    @GetMapping("/getCurrentUserInfo")
    public Result getCurrentUserInfo(@RequestHeader String token) {
        String userId = JwtHelper.getUserId(token);
        User user;
        try {
            user = userService.getById(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail().message("获取用户信息失败");
        }
        return Result.ok(user);
    }

    @Operation(summary = "根据token修改用户信息")
    @PutMapping("/updateCurrentUserInfo")
    public Result updateCurrentUserInfo(@RequestHeader String token, @RequestBody User user) {
        String userId = JwtHelper.getUserId(token);
        user.setId(userId);
        if(StringUtils.hasText(user.getPassword())){
            user.setPassword(MD5.encrypt(user.getPassword()).toLowerCase());
        }
        user.setModifyTime(TimeUtils.currentDate());
        try {
            userService.updateById(user);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail().message("更新失败");
        }
        return Result.ok();
    }

    @Operation(summary = "校验当前用户输入的旧密码是否正确")
    @PostMapping(value = "/checkUserPassword")
    public Result checkUserPassword(@RequestHeader String token,@RequestBody User user){
        String userId = JwtHelper.getUserId(token);
        boolean res = userService.checkCurrentUserPwd(userId,user.getPassword());
        if(!res){
            throw new UserPasswordException(ResultCodeEnum.CHECK_PASSWORD_ERROR);
        }
        return Result.ok();
    }
}
